<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/views/shared/taglib.jsp"%>
<%
	request.setAttribute("modelConfigPath", "stock");
%>
<%@ include file="/WEB-INF/views/shared/master/contentHeader.jsp"%>
<style >
	#outStockInfoCopy{display:none}
	td input{height:21px; width:98%;border:none;}
	td{height:21px;padding: 2px 2px !important;text-align:center }
	#area{
		height:340px;
		overflow-y:auto; 
		overflow-x:hidden; 
	}

</style>	
<t:panel cssClass="panel-flat form-detailed" customBody="true" customHead="true">
    <div class="detailed-navbar-content panel-body">
    	
        <t:form modelAttribute="inStock" action="/purchase/returnPurchase/saveOutStock" method="post" cssClass="form-horizontal">
            <div class="hide">
                <t:hidden path="inStockId"/>
                <t:hidden path="deleteMark"/>     
            </div>
            <div class="detailed-navbar-content panel-body">
	             <t:formgroup>
	                <t:label path="supplierId" cssClass="col-sm-1" />
	                <div  class="col-sm-3 col-xs-3">
						<div class="input-group ">
							<span onclick="addSupplier()" id="switch" class="input-group-addon"><i class="icon-add"></i></span>
							<t:select2 id="supplierId" path="supplierId" placeholder="供应商" allowClear="true" ></t:select2>
							<input class="form-control"  id="supplierId" placeholder="请选择供应商" type="text" style="display:none" ></input>
						</div>
					</div>
	                <t:label path="inStockNo" cssClass="col-sm-1" />
	                <t:input path="inStockNo" colClass="col-sm-3" />   
	              	<t:label path="operatorName" cssClass="col-sm-1" />
	                <t:input path="operatorName" colClass="col-sm-3" />
	                <!-- <t:label path="warehouse" cssClass="col-sm-1" />
	                <t:select2 path="warehouse" allowClear="true" placeholder="请选择仓库"  colClass="col-xs-2" /> -->
	            </t:formgroup>
	            <t:formgroup>
	               	
	                <t:label path="createDate" cssClass="col-sm-1" />
	                <t:date  inputGroupIcon="icon-calendar" path="createDate" colClass="col-sm-3"></t:date>
	                 <t:label path="comment" cssClass="col-sm-1" />
	               	<t:input path="comment" colClass="col-sm-7" /> 
	               
	            </t:formgroup>
	       
	  		</div>
        <div id="area">   
				<table class="cntable table table-bordered dataTable " >
					<thead>
						<tr class="success" >
							<th style="width:50px">		
							</th>
							<th style="width:70px">序号</th>
							<th style="width:150px">商品编码</th>
		            		<th style="width:200px">商品名称</th>
							<th style="width:100px">规格</th>
							<th style="width:100px">单位</th>
							<th style="width:100px">退货数量</th>
							<th style="width:120px">退货单价(元)</th>
							<th>小计(元)</th>
						</tr>
					</thead>
					<tbody onclick="checkSupplier()" id="outStockInfo">
						<tr id="outStockCollocation_0" name="outStockCollocation">
							<td style="display:none"><input id="collocationIds_0" name="collocationIds" type="text" value=""/></td>
							<td style="display:none"><input id="cargoId_0" name="cargoId" type="text" value=""/></td>
							<td class="cargoType" style="display:none"><input id="cargoType_0" name="cargoType" type="text" value=""/></td>
							<td name="index"></td>
							<td id="logo" >
				            	<div class="col-md-6 col-sm-6" onclick="addData()" ><i class="fa fa-plus-square"></i></div>
				            	<div class="col-md-6 col-sm-6" onclick="deleteData('outStockCollocation_0')"><i class="fa fa-minus-square"></i></div>
			            	</td>
			            	<td colspan="2">
								<select style="ime-mode:disabled;" id="cargoNo_0" name="cargoNo" type="text" value=""><option value=""></option></select>
							</td>
							<td style="display:none;"><input style="ime-mode:disabled;" disabled="disabled" id="cargoNoShow_0" name="cargoNoShow" type="text" value=""/></td>
			            	<td style="display:none;"><input style="ime-mode:disabled" disabled="disabled" id="cargoName_0" name="cargoName" type="text" value=""/></td>
			            	<td><input style="ime-mode:disabled" readonly="true" id="propertyName_0" name="propertyName" type="text" value=""/></td>
			            	<td >
			            		<select style="ime-mode:disabled;" id="unit_0" name="unit" type="text" value=""></select>
			            		
			            	</td>
							
							<td><input class="form-control" style="ime-mode:disabled"  onkeyup="return ValidateNumber(this,value)" id="purchaseNums_0"  name="inStockNums" type="text" value=""/></td>
							<td><input class="form-control" onchange="change(this)" style="ime-mode:disabled"  onkeyup="return ValidateNumber(this,value)" id="purchasePrices_0" name="inStockPrices" type="text" value=""/></td>
							<td><input class="form-control" style="ime-mode:disabled"  id="subtotal_0" name="subtotal" type="text" value=""/></td>
						</tr>
						<c:set var="sequence"  value="0"/>
					</tbody>
					<tfoot >
						<tr  >
							<td>合计</td>
							<td></td>
							<td colspan="4"> </td>
							<td><input id="numSubtotal" readonly="true" type="number" style="background:none;outline:none;border:0px"  placeholder="0"></td>
							<td> </td>
							<td><input id="moneySubtotal" readonly="true" type="number" style="background:none;outline:none;border:0px"  placeholder="0.0"></td>
						</tr>
					</tfoot>
				</table>     
				<p id="notice" style="color:red;display:none" >商品不能为空并要填写对应的数量和单价</p>     
			</div>
	  		
        </t:form>
    </div>
    <t:panelFoot headingBtnCssClass="heading-btn pull-right-important">
		<t:button icon="icon-floppy-disk" onclick="saveData()" laddaType="progress" cssClass="btn-info" csize="sm">保存</t:button>
	</t:panelFoot>
</t:panel>

<table id="outStockInfoCopy">
	<tbody id="outStockInfoCopyBody">
		<tr id="outStockCollocation_temp" name="outStockCollocation" >
			<td style="display:none"><input id="collocationIds_temp" name="collocationIds" type="text" value=""/></td>
			<td style="display:none"><input id="cargoId_temp" name="cargoId" type="text" value=""/></td>
			<td class="cargoType" style="display:none"><input id="cargoType_temp" name="cargoType" type="text" value=""/></td>
			<td name="index"></td>
			<td id="logo" >
		          	<div class="col-md-6 col-sm-6" onclick="addData()" ><i class="fa fa-plus-square"></i></div>
		          	<div class="col-md-6 col-sm-6" onclick="deleteData('outStockCollocation_temp')"><i class="fa fa-minus-square"></i></div>
		     </td>
								
         	<td colspan="2"><select style="ime-mode:disabled" id="cargoNo_temp" name="cargoNo" type="text" value=""><option value=""></option></select></td>
         	<td style="display:none;"><input style="ime-mode:disabled;" readonly="true" id="cargoNoShow_temp" name="cargoNoShow" type="text" value=""/></td>
         	<td style="display:none;"><input style="ime-mode:disabled" readonly="true" id="cargoName_temp" name="cargoName" type="text" value=""/></td>
         	<td><input style="ime-mode:disabled" readonly="true" id="propertyName_temp" name="propertyName" type="text" value=""/></td>
			<td >
				<select  style="ime-mode:disabled;"id="unit_temp" name="unit" type="text" value=""></select>
			</td>
	
			<td class="price"><input class="form-control" style="ime-mode:disabled"  onkeyup="return ValidateNumber(this,value)" id="purchaseNums_temp"  name="inStockNums" type="text" value=""/></td>
			<td class="price"><input class="form-control" onchange="change(this)" style="ime-mode:disabled"  onkeyup="return ValidateNumber(this,value)" id="purchasePrices_temp" name="inStockPrices" type="text" value=""/></td>
			<td class="price"><input class="form-control" style="ime-mode:disabled" id="subtotal_temp" name="subtotal" type="text" value=""/></td>
		</tr>
	</tbody>
</table>

<script type="text/javascript">

$(function(){
	
	//注册下拉框选择展开监听事件
	$("#supplierId").on("select2:open", function (e) { 
		var suppliers=[];
		var url =$.getVirtualPath()+"/shop/supplier/supplierList";
		 $.ajax({
		        type: 'get',
		        dataType: "json",
		        url: url,
		        async: false,
		        success: function (result) {
		        	data=[];			
					if(!$.isNull(result)){
						data = result;
						data.forEach(function(element,index){
							suppliers.push({
								id:element.supplierId,
								text:element.supplierName+"("+element.supplierNo+")"
							})
						})
					}
		        }
	    });
		var select=this;
		var supplierOlds=[];
		var newList=[];
		$(select).find("option").each(function(){
			var option=$(this).val();
			var opText=$(this).text();
			
			supplierOlds.push({
				id:option,
				text:opText
			});
		})
		for(var i=0;i<suppliers.length;i++){
			var newSupplier=suppliers[i]
			var isExist = false;
			for(var j=0;j<supplierOlds.length;j++){
				var oldSupplier=supplierOlds[j];
				if(oldSupplier.id==newSupplier.id){
					isExist=true;
					break;
				}
			}
			if(!isExist){
				newList.push(newSupplier);
		    }
		
			
		}
		if(!$.isNull(newList)){
			newList.forEach(function(ele,index){
				var newOption = new Option(ele.text, ele.id, false, false);
				
				$(select).append(newOption);
			})
			
		}
		
		console.log(supplierOlds);

	});
	
	
	outStockInfo = $("#outStockInfoCopyBody").html();
	randomNumber();
	var productNo=$('#productNo').text();
	if(!$.isNull(productNo)){
	 showMark=true;
	}
	loadCargoInfo();
	$('#otherMoney').bind('input propertychange', function() {
		var otherMoney = parseFloat($(this).val());

		if(isNaN(otherMoney)){
			otherMoney=0
			$(this).val("");
		}
		updateBillingMoney();
	});
	stocksShow(0);
	indexChange();
	
	iframeLazyOpen();
	tabpaneResize();
	
})
var productInfoData = [];
var unitInfo=[];
var unitInfoForChange=[];
var unitListForEdit=[];
var changeNum=0;
function loadCargoInfo(){
	$("#supplierId").change(function(){
		var collocationIds=[];
		var showConfirm=false;
		$("input[name='collocationIds']").each(function(){
			collocationIds.push({
				id:$(this).val(),
				text:$(this).val()
			})
		});

		for(var i=0;i<collocationIds.length;i++){
			if(collocationIds[i].id!=""){
				showConfirm=true
			}
		}
		changeNum++;
		if(showConfirm){
			if(changeNum>1){
				confirm("是否确认修改供应商？修改后，将清除已选择数据。",function(result){
	    			if(result){
	    				loadPurchase();
	    			}else{
	    				return;
	    			}
	    		});
			}else{
				loadPurchase();
			}
		}else{
			loadPurchase();
		}

	})
}

function addSupplier(){
	top.openModal("/shop/supplier/supplierDetailed?type="+"add","supplier_form","供应商详情-添加",900,500);
}

function loadPurchase(){
	var supplierId=$("#supplierId").find("option:selected").val();
	productInfoData=[];
	unitInfo=[];
	cargoInfo=[];
	var url =$.getVirtualPath()+"/purchase/purchase/supplierSelect";
	 $.ajax({
	        type: 'get',
	        dataType: "json",
	        url: url,
	        data: "supplierId="+supplierId,
	        async: false,
	        success: function (result) {
	        	data=[];			
				if(!$.isNull(result)){
					data = result;
					cargoInfo=result;
					productInfoData.push({
    					id:"1",
    					text:"",
    					cargoId:"",
    					cargoName:"",
    					cargoNo:"",
    					cargoType:"",
    					propertyName:"",
    					unit:"",
    					supplyPrice:"",
    					viceFirstUnit:"",
						viceSecondUnit:"",
						firstRatio:"",
						secondRatio:"",
    				});
				
					result.forEach(function(element,index){		    			
						puttyNum = 0;
						
				
							 if(!$.isNull(element.collocation.propertyNameTwo)){
		    					 productInfoData.push({
		    						 id:element.collocation.collocationId,
										text:element.cargo.cargoNo+" "+element.cargo.cargoName+" "+"【"+element.collocation.propertyNameOne+"*"+element.collocation.propertyNameTwo+"】",
										cargoId:element.cargoId,
										cargoName:element.cargo.cargoName,
										cargoNo:element.cargo.cargoNo,
										cargoType:element.cargo.cargoType,
										unit:element.cargo.unit,
										viceFirstUnit:element.cargo.viceFirstUnit,
										viceSecondUnit:element.cargo.viceSecondUnit,
										firstRatio:element.cargo.firstRatio,
										secondRatio:element.cargo.secondRatio,
										propertyName:element.collocation.propertyNameOne + "/" + element.collocation.propertyNameTwo,
										supplyPrice:element.supplyPrice,
				    				})
		    				 }else{
		    					 productInfoData.push({
		    						 id:element.collocation.collocationId,
										text:element.cargo.cargoNo+" "+element.cargo.cargoName+" "+"【"+element.collocation.propertyNameOne+"】",
										cargoId:element.cargoId,
										cargoName:element.cargo.cargoName,
										cargoNo:element.cargo.cargoNo,
										cargoType:element.cargo.cargoType,
										unit:element.cargo.unit,
										viceFirstUnit:element.cargo.viceFirstUnit,
										viceSecondUnit:element.cargo.viceSecondUnit,
										firstRatio:element.cargo.firstRatio,
										secondRatio:element.cargo.secondRatio,
										propertyName:element.collocation.propertyNameOne ,
										supplyPrice:element.supplyPrice,
				    				})
		    				 }
							
						}); 
					}
	        }
	    });
	
 	 var select=$('#cargoNo_'+0).select2({placeholder:"请选择货物信息",allowClear: true ,data : productInfoData});
 	var select2=$('#unit_'+0).select2({placeholder:"请选择单位",allowClear: true ,data : productInfoData});
	 deleteAll(); 
}



function deleteAll(){
	$("tr[name='outStockCollocation']").each(function(){
		if($(this).attr("id")!="outStockCollocation_temp"){
			$(this).remove();
			updateBillingMoney();
			updateNumSubtotal();
			indexChange();
		}
	});
	num=0;
	newOutStockInfo=outStockInfo.replace(/1/g,num+1);
	newOutStockInfo=newOutStockInfo.replace(/temp/g,num);
	
	$("#outStockInfo").append(newOutStockInfo);
	stocksShow(num);
	indexChange();
	
}

function checkSupplier(){
	var supplierId=$("#supplierId").find("option:selected").val();	
	if($.isNull(supplierId)){		
		$.showTips("请选择供应商！",1000,"warning");
		return;
	}
}



function iframeLazyOpen(){
	$(".nav.nav-tabs.nav-tabs-component li a").click(function(){
		var iframeTab = $(this).attr("href");
		var iframeId = iframeTab.substring(0,iframeTab.indexOf("-")) + "Iframe";
		$(iframeId).attr("src",$(iframeId).attr("data-src"));
	});
}

function tabpaneResize(){
	$(".tab-pane").height($(window).height() - 50);
	$(window).bind("resize",function(){$(".tab-pane").height($(window).height() - 50)});
}

var outStockInfo;
var newOutStockInfo;
var num=0;
function addData(){
	num++;
	newOutStockInfo=outStockInfo.replace(/1/g,num+1);
	newOutStockInfo=newOutStockInfo.replace(/temp/g,num);
	
	$("#outStockInfo").append(newOutStockInfo);
	stocksShow(num);
	indexChange();
}

function deleteData(deleteId){
	if(index!=3){
		console.log(index);
		$("#"+deleteId).remove();
		updateBillingMoney();
		updateNumSubtotal();
		indexChange();
	}
}

var index;
function indexChange(){
	index=1;
	$("td[name='index']").each(function(){
		$(this).html(index);	
		index++;
	});
}
function saveData(lBtn){
	var isRepetition = false;
	var collocationIds=[];
	$("input[name='collocationIds']").each(function(){
		collocationIds.push({
			id:$(this).val(),
			text:$(this).val()
		})
	});

	for(var i=0;i<collocationIds.length;i++){
		console.log(collocationIds[i].id);
		for(var j=i+1;j<collocationIds.length;j++){
			
			if(collocationIds[i].id!=""||collocationIds[j].id!=""){
				if(collocationIds[i].id==collocationIds[j].id){
					isRepetition=true;
				}
			}

		}
	}
	if(isRepetition){
		$.showTips("存在相同货品！",1000,"warning");
		return;
	}
	
	var isSave=false;
	$("input[name='collocationIds']").each(function(){
		if($(this).val()!=null && $(this).val() != ""){
			isSave=true;
		}
						
	});
	if(!isSave){
		$.showTips("请选择开单货品！",1000,"warning");
		return;
	}

	if (!$("form:first").valid()){
		return;
	}
	lBtn.start();
	$("form:first").submit();
}	

var showMark=true;

var stuffData=[];
function stocksShow(showNum){
	unitInfo=[];
	var select=$('#cargoNo_'+showNum).select2({placeholder:"请选择货物信息",allowClear: true ,data : productInfoData});
	var select2=$('#unit_'+showNum).select2({placeholder:"请选择单位",allowClear: true ,data : unitInfo});
	
	select.change(function(){
		unitInfoForChange=[];
		var collocationId = $('#cargoNo_'+showNum).val();
		var repetitionNum = 0;
		$("select[name='cargoNo']").each(function(){
			if($(this).val()!=null&&$(this).val()!=""){
				if($(this).val()==collocationId){
					repetitionNum++;
				}
			}

		});
		
		
		productInfoData.forEach(function(element,index){
			if(element.id==collocationId){
				if(!$.isNull(element.unit)){
					unitInfo.push({
						id:element.unit,
						text:element.unit,
						ratio:"1",
						mark:element.id
					})
				}
				
				if(!$.isNull(element.viceFirstUnit)){
					unitInfo.push({
						id:element.viceFirstUnit,
						text:element.viceFirstUnit+"（"+element.firstRatio+"）",
						ratio:element.firstRatio,
						mark:element.id
					})
				}
				if(!$.isNull(element.viceSecondUnit)){
					unitInfo.push({
						id:element.viceSecondUnit,
						text:element.viceSecondUnit+"（"+element.secondRatio+"）",
						ratio:element.secondRatio,
						mark:element.id
					})
				}
				unitInfoForChange=unitInfo;
				unitListForEdit.push(unitInfoForChange)
				console.log(unitListForEdit.length+"MMMMMMM")
				$('#cargoId_'+showNum).val(element.cargoId);
				$('#collocationIds_'+showNum).val(element.id);
				$('#cargoNoShow_'+showNum).val(element.cargoNo);
				$('#cargoName_'+showNum).val(element.cargoName);
				$('#cargoType_'+showNum).val(element.cargoType);
				
				$('#unit_'+showNum).val(element.unit);
				$('#propertyName_'+showNum).val(element.propertyName);
				$('#purchaseNums_'+showNum).val(1);
				if(element.supplyPrice!=null && element.supplyPrice != ""){
					$('#purchasePrices_'+showNum).val(element.supplyPrice);
					$('#subtotal_'+showNum).val(element.supplyPrice*1);
				}else{
					$('#purchasePrices_'+showNum).val(0);
					$('#subtotal_'+showNum).val(0);
				}
				
				var select2=$('#unit_'+showNum).select2({placeholder:"请选择单位",allowClear: true ,data : unitInfo});
				select2.change(function (){
					var unit=$(this).val();

					if(unitListForEdit.length>0){
						
						for(var i=0;i<unitListForEdit.length;i++){
								for(var j=0;j<unitListForEdit[i].length;j++){
									if(unitListForEdit[i][j].mark==collocationId){
										if(unitListForEdit[i][j].id==unit){
											
											$('#purchasePrices_'+showNum).val(element.supplyPrice*unitListForEdit[i][j].ratio);
											
											var count=$('#purchaseNums_'+showNum).val();
											$('#subtotal_'+showNum).val(element.supplyPrice*unitListForEdit[i][j].ratio*count);
										}
								}
							}
						}
					}
					
					
					updateBillingMoney();
					updateNumSubtotal();
					
				})
				
			}
		});
		
		var cargoType=$('#cargoType_'+showNum).val()
		
		
		if(cargoType!="库存商品"){
			
			$('#cargoId_'+showNum).parent().siblings(".price").find("input").css("color","red")
			$('#unit_'+showNum).css("color","red");
		}
		
		$("#cargoNoShow_"+showNum).parent().css("display","");
		$("#cargoName_"+showNum).parent().css("display","");
		$("#cargoNo_"+showNum).parent().css("display","none");
		updateBillingMoney();
		updateNumSubtotal();
		if(repetitionNum>1){
			$.showTips("此货品已存在！",1000,"warning");
			deleteData('outStockCollocation_'+showNum);
		}
		addNewRow();
	});
	updateSubtotal(showNum);
	
	
}



function addNewRow(){
	
	var isAllow=0;
	var collocationIds=[];
	$("input[name='collocationIds']").each(function(){
		collocationIds.push({
			id:$(this).val(),
			text:$(this).val()
		})
	});

	for(var i=0;i<collocationIds.length;i++){
		if(collocationIds[i].id==""){
			isAllow++;
			console.log(isAllow);
		}
	}
	if(isAllow<2){
		addData();
	}
	
}

function updateSubtotal(showNum){
	$('#purchaseNums_'+showNum).bind('input propertychange', function() {
		var count = parseInt($(this).val());
		if(isNaN(count)){
			count = 0;
			$(this).val(0);
		}else{
			var numValue = $(this).val();
			if(numValue.length>1){
				if(numValue.substring(1,2)!="."){
					if(numValue.substring(0,1)=='0'){
						$(this).val(parseFloat(numValue.substring(1,numValue.length)));
					}
				}

			}
		}
		var price = $('#purchasePrices_'+showNum).val();
		$('#subtotal_'+showNum).val(price*count);
		updateBillingMoney();
		updateNumSubtotal();
		
	});
	$('#purchasePrices_'+showNum).bind('input propertychange', function() {
		var price = parseFloat($(this).val());
		
		if(isNaN(price)){
			price = 0;
			$(this).val(0);
		}else{
			var numValue = $(this).val();
			if(numValue.length>1){
				if(numValue.substring(1,2)!="."){
					if(numValue.substring(0,1)=='0'){
						$(this).val(parseFloat(numValue.substring(1,numValue.length)));
					}
				}

			}
		}
		
		var count = $('#purchaseNums_'+showNum).val();
		if($(this).parent().siblings(".cargoType").find("input").val()!="库存商品"){
			$(this).val(price)
			$('#subtotal_'+showNum).val(-price*count);
			updateBillingMoney();
		}else{
			$('#subtotal_'+showNum).val(price*count);
			updateBillingMoney();
		}
		
	});
	$('#purchaseNums_'+showNum).focus();
	
}

function updateBillingMoney(){
	var purchaseMoney = 0;
	var moneySubtotal=0;
	var otherMoney = $("#otherMoney").val();
	if(otherMoney != "" && otherMoney != null && otherMoney != undefined && otherMoney!=0){
		purchaseMoney=purchaseMoney+parseFloat(otherMoney);
	}

	$("input[name='subtotal']").each(function(){
		if($(this).val()!=null && $(this).val() != ""){
			purchaseMoney+= parseFloat($(this).val());
			moneySubtotal+= parseFloat($(this).val());
		}
						
	});
	$("#purchaseMoney").val(purchaseMoney);
	$("#moneySubtotal").val(moneySubtotal);
    
}

function updateNumSubtotal(){
	var numSubtotal=0;
	$("input[name='inStockNums']").each(function(){
		if($(this).val()!=null && $(this).val() != ""){
			numSubtotal+= parseFloat($(this).val());
		}
						
	});
	$("#numSubtotal").val(numSubtotal);
}

function ValidateNumber(e, pnumber){
	if (!/^\d+[.]?\d*$/.test(pnumber))
	{
	e.value = /^\d+[.]?\d*/.exec(e.value);
	}else{
		
 	}
	return false;
}
function change(element){
	var e=element.value
	if($(element).parent().siblings(".cargoType").find("input").val()!="库存商品"){
		$(element).val(-e)
	}
	
}

function randomNumber(){
	const now =new Date();
	
	var  month=now.getMonth()+1;
	var day=now.getDate();
	var hour=now.getHours();
	var minutes=now.getMinutes();
	var seconds=now.getSeconds();
    var inStockNo="CG-R-"+ now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds
	$("#inStockNo").val(inStockNo);
	
}

function addPurchaseCollocation(){
	var supplierId=$("#supplierId").find("option:selected").val();	
	if($.isNull(supplierId)){		
		$.showTips("请选择供应商！",1000,"warning");
		return;
	}
	top.openModal("/shop/supplyCargo/collocation/index?supplierId="+$("#supplierId").val()+"&type="+"addPurchaseCollocation","supplyCargo_form","商品-添加",1000,500);
}
function assetModalClose(){
	console.log("bbb");
	var supplierId=$("#supplierId").find("option:selected").val();
	var newProductInfoData=[];
	cargoInfo=[];
	var url =$.getVirtualPath()+"/purchase/purchase/supplierSelect";
	$.ajax({
		type: 'get',
		dataType: "json",
		url: url,
		data: "supplierId="+supplierId,
		async: false,
		success: function (result) {
	     	data=[];			
			if(!$.isNull(result)){
				data = result;
				cargoInfo=result;
				result.forEach(function(element,index){		    			
					puttyNum = 0;

					newProductInfoData.push({
	  					id:element.collocation.collocationId,
	  					text:element.cargo.cargoNo+" "+element.cargo.cargoName+" "+"【"+element.collocation.propertyNameOne+"*"+element.collocation.propertyNameTwo+"】",
	  					cargoId:element.cargoId,
	  					cargoName:element.cargo.cargoName,
	  					unit:element.cargo.unit,
	  					cargoNo:element.cargo.cargoNo,
	  					cargoType:element.cargo.cargoType,
	  					propertyName:element.collocation.propertyNameOne + "/" + element.collocation.propertyNameTwo,
	  					supplyPrice:element.supplyPrice,
	    			})
				}); 
			}
		}
	});
	
	for(var j=0;j<productInfoData.length;j++){
		for(var i=0;i<newProductInfoData.length;i++){
			if(newProductInfoData[i].id==productInfoData[j].id){
				newProductInfoData.splice(i,1);
				
				i=i-1;
			} 
		}
	}
	newProductInfoData.forEach(function(element,index){
		productInfoData.push({
			id:element.id,
			text:element.text,
			cargoId:element.cargoId,
			cargoName:element.cargoName,
			cargoNo:element.cargoNo,
			cargoType:element.cargo.cargoType,
			unit:element.unit,
			propertyName:element.propertyName,
			supplyPrice:element.supplyPrice,
		})
	});
	$("select[name='cargoNo']").each(function(){
		if($(this).attr("id")!="cargoNo_temp"){
			var select=$(this).select2({placeholder:"请选择货物信息",allowClear: true ,data : newProductInfoData});
		}
	});

}
</script>
<%@ include file="/WEB-INF/views/shared/base/scripts/jqueryui.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formtags.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formbuttons.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formpickers.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formselects.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formstyling.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/dialog.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/contentFooter.jsp"%>
